const App = {
  data() {
    return {
      nodeKey: '',
      tableData: [],
      treeData: [],
      Model_Name: '',
      loading: false,
      load: false,
      cloneTreeData: [],
      relativePath: ''
    }
  },
  mounted() {
    let ls_URL = window.location.href;
    this.nodeKey = getQueryObject(ls_URL, "nodeKey");// 获取 nodeKey 参数
    this.relativePath = ls_URL.substring(
      ls_URL.lastIndexOf("view"),
      ls_URL.length
    );
    this.getData();
  },
  methods: {
    check() {
      this.cloneTreeData = JSON.parse(JSON.stringify(this.tableData));
      let lArray_TreeData = this.cloneTreeData;
      this.treeData = this.Recursion(lArray_TreeData);
    },
    Recursion(lArray_TreeData) {
      lArray_TreeData.forEach(lJSONObject_Record => {//1级
        //获取对象中子节点
        let lJSONArray_Children = lJSONObject_Record.children;
        if (!lJSONArray_Children) {
          let ls_ModleName = lJSONObject_Record.Model_Name;
          if (!ls_ModleName.includes(this.Model_Name)) {
            lArray_TreeData = lArray_TreeData.filter(i => i.Model_Number != lJSONObject_Record.Model_Number)
          }
        } else {
          lJSONArray_Children = this.Recursion(lJSONArray_Children);
          //判断子节点长度，若子节点长度大于0则处理好的子节点更新
          if (lJSONArray_Children.length > 0) {
            lJSONObject_Record.children = lJSONArray_Children
          } else {//将该子节点删除
            lArray_TreeData = lArray_TreeData.filter(i => i.Model_Number != lJSONObject_Record.Model_Number);
          }
        }
      })
      return lArray_TreeData;
    },
    handleSubmit(row) {
      if (this.load) {
        row.loading = true;
        $.ajax({
          headers: requestHeaders(),
          url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
          type: "POST",
          data: {
            Data: "base_system_rights_user_model",
            Condition: "Model_Number='" + row.Model_Number + "' AND User_Number='" + this.nodeKey + "'",
          },
          success: (data) => {
            let lJSONObject_DataSet;
            if (data.length > 0) { //修改
              data[0].UR_Page = row.UR_New + row.UR_Del + row.UR_Modify + row.UR_Query;
              data[0].UR_New = row.UR_New;
              data[0].UR_Del = row.UR_Del;
              data[0].UR_Modify = row.UR_Modify;
              data[0].UR_Query = row.UR_Query;
              lJSONObject_DataSet = {
                Modify: {
                  base_system_rights_user_model: data
                }
              }
            } else {//新增
              lJSONObject_DataSet = {
                Add: {
                  base_system_rights_user_model: [{
                    Model_Number: row.Model_Number,
                    User_Number: this.nodeKey,
                    UR_Page: row.UR_New + row.UR_Del + row.UR_Modify + row.UR_Query,
                    UR_New: row.UR_New,
                    UR_Del: row.UR_Del,
                    UR_Modify: row.UR_Modify,
                    UR_Query: row.UR_Query
                  }]
                }
              }
            }
            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
            if (!lJSONObject_DataSet['Add']) {
              lJSONObject_DataSet['Add'] = new Object();
            }
            lJSONObject_DataSet['Add']['base_operation_log'] = [{
              Operation_Type: '0',
              Operation_Path: this.relativePath,
              Operation_Main,
              Operation_Interface: "generalSaveByRequestBody",
              Operation_IP: getIpAddress()
            }]
            submitSave(lJSONObject_DataSet, (res) => {
              if (res.Status != 'success') {
                this.$message.error('操作失败！请重试。');
              }
              row.loading = false;
            })
            // $.ajax({
            //   headers: requestHeaders(),
            //   url: `${mh}/generalBusiness/generalSaveByRequestBody`,
            //   method: "post",
            //   data: JSON.stringify({
            //     Data: lJSONObject_DataSet,
            //   }),
            //   success: (res) => {
            //     if (res.Status != 'success') {
            //       this.$message.error('操作失败！请重试。');
            //     }
            //   },
            //   complete: (XMLHttpRequest) => {
            //     row.loading = false;
            //     requestPretreatment(XMLHttpRequest)
            //   }
            // });
          },
          complete: (XMLHttpRequest) => {
            requestPretreatment(XMLHttpRequest)
          },
          dataType: "json",
        });
      }
    },
    getData() {
      this.loading = true;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        type: "POST",
        data: {
          Data: "base_system_rights_user_model",
          Condition: "User_Number='" + this.nodeKey + "'",
        },
        success: (rights) => {
          $.ajax({
            headers: requestHeaders(),
            url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
            type: "GET",
            data: {
              TableName: 'base_system_model_list',
              NodeCode: ''
            },
            success: (data) => {
              this.configData(data, rights);
              this.tableData = data;
              this.treeData = data;
              this.loading = false;
              this.$nextTick(() => {
                this.load = true;
              })
            },
            complete: (XMLHttpRequest) => {
              requestPretreatment(XMLHttpRequest)
            },
            dataType: "json",
          });
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest)
        },
        dataType: "json",
      })
    },
    configData(data, rights) {
      data.forEach(item => {
        let obj = rights.filter(i => i.Model_Number == item.Model_Number)[0]
        if (obj) {
          item.UR_New = obj.UR_New;
          item.UR_Del = obj.UR_Del;
          item.UR_Modify = obj.UR_Modify;
          item.UR_Query = obj.UR_Query;
        } else {
          item.UR_New = '0';
          item.UR_Del = '0';
          item.UR_Modify = '0';
          item.UR_Query = '0';
        }
        item.loading = false;
        if (item.children && item.children.length > 0) {
          this.configData(item.children, rights);
        }
      })
    },
    goBack() {
      window.history.back();
    },
  },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
